import pygame
from pygame.sprite import Group

from ship import Ship


class Scoreboard:
    def __init__(self,state,ai_setting,screen):
        self.game_state = state;
        self.setting = ai_setting;
        self.screen = screen;

        # 得分字体设置
        self.text_color = (30, 30, 30);
        self.font = pygame.font.SysFont('arial', 48);

        # 初始化得分图像
        self.prep_score()
        self.prep_height_score()
        self.prep_level()
        self.prep_ships();

    def prep_score(self):
        round_score = int(round(self.game_state.score,-1))
        score_str = "{:,}".format(round_score)
        self.score_image = self.font.render(score_str,True,self.text_color,self.setting.bg_color);

        # 将得分放在屏幕右上角：
        self.score_rect = self.score_image.get_rect();
        self.score_rect.right = self.screen.get_rect().right - 20;
        self.score_rect.top = 20;

    def prep_height_score(self):
        height_score = int(round(self.game_state.height_score, -1))
        height_score_str = "{:,}".format(height_score);
        height_score_str = "max score:"+ height_score_str;
        self.height_score_image = self.font.render(height_score_str,True,self.text_color,self.setting.bg_color);

        #将最高分房子中间
        self.height_score_rect = self.height_score_image.get_rect();
        self.height_score_rect.centerx = self.screen.get_rect().centerx;
        self.height_score_rect.top = 20;

    def prep_level(self):
        self.level_iamge = self.font.render('level:'+ str(self.game_state.level), True, self.text_color, self.setting.bg_color);
        #级别
        self.level_rect = self.level_iamge.get_rect();
        self.level_rect.left = self.screen.get_rect().left + 20;
        self.level_rect.top = 20
    def prep_ships(self):
        self.ships = Group();
        for index in range(self.game_state.ship_left):
            ship = Ship(self.setting,self.screen);
            ship.rect.x = 10 + index * ship.rect.width;
            ship.rect.y = self.screen.get_rect().bottom - ship.rect.height - 10;
            self.ships.add(ship);

    def show_score(self):
        self.screen.blit(self.score_image,self.score_rect);
        self.screen.blit(self.height_score_image,self.height_score_rect);
        self.screen.blit(self.level_iamge,self.level_rect)
        self.ships.draw(self.screen);